package com.my12306.user.service.auth.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.user.dao.auth.UserRoleDao;
import com.my12306.user.model.po.auth.UserRoleEntity;
import com.my12306.user.service.auth.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRoleEntity> implements UserRoleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());
        if (page != null && page < 1) {
            page = 1;
            limit = 10;
        }
        IPage<UserRoleEntity> iPage = new Page(page, limit);
        IPage<UserRoleEntity> pg = this.page(iPage, new QueryWrapper<UserRoleEntity>());
        return new PageUtils(pg);
    }

    @Override
    public List<Long> selectRoleIdByUserId(Long userId) {
        List<Long> roleId = this.baseMapper.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id", userId)).stream()
                .map(x -> {
                    return x.getRoleId();
                }).collect(Collectors.toList());
        return roleId;
    }

    public List<UserRoleEntity> createUserRole(List<Long> roleIds, Long userId) {
        if (roleIds.size() > 0 && userId != null) {
            return roleIds.stream().parallel().map(x -> {
                UserRoleEntity roleEntity = new UserRoleEntity();
                roleEntity.setRoleId(x);
                roleEntity.setUserId(userId);
                roleEntity.setCreateDate(new Date());
                roleEntity.setUpdateDate(new Date());
                return roleEntity;
            }).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    @Transactional
    public ResponseData deleteUserRoleByIds(List<Long> ids) {
        System.out.println(ids);
        this.baseMapper.deleteBatchIds(ids);
        return ResponseData.ok();
    }

    @Override
    @Transactional
    public ResponseData deleteUserRoleByUserIds(List<Long> ids) {
        System.out.println("ids=" + ids);
        if (ids != null && ids.size() > 0) {
            this.baseMapper.deleteUserRoleByUserIds(ids);
            return ResponseData.ok();
        }
        return ResponseData.fail();
    }

    @Override
    @Transactional
    public ResponseData deleteUserRoleByRoleIds(List<Long> ids) {
        this.baseMapper.deleteBatchUserRoleByRoleIds(ids);
        return ResponseData.ok();
    }

    /**
     * 判断是否是管理员，如果是返回True ，否则返回false
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean isEmptyAdminByUserId(Long userId) {
        String roleName = this.baseMapper.queryRoleByUserId(userId);
        if (CommonConstants.ADMIN.equals(roleName)) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean saveRoleUser(List<UserRoleEntity> userRoleEntities) {
        if (userRoleEntities != null && userRoleEntities.size() > 0) {
            userRoleEntities = userRoleEntities.parallelStream().map(x -> {
                x.setId(SnowflakeIdWorker.generateId());
                return x;
            }).collect(Collectors.toList());
            System.out.println(this.baseMapper.saveBatchUserRole(userRoleEntities));
            return true;
        }
        return false;
    }
}
